<template>
  <div class="app-container">
    <a-title @clickPreview="preview" />
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="100px">
      <el-form-item label="项目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入项目名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="viewList"
        />
      </el-form-item>
      <el-form-item label="项目代码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入项目代码"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="viewList"
        />
      </el-form-item>
      <el-form-item label="开始服务日期" label-width="100px">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="委托单位" prop="party">
        <el-input
          v-model="queryParams.party"
          placeholder="请输入委托单位"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="viewList"
        />
      </el-form-item>
      <el-form-item label="项目负责人" prop="projectLeaderName" label-width="100px">
        <el-input
          v-model="queryParams.projectLeaderName"
          placeholder="请输入项目负责人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="viewList"
        />
      </el-form-item>
      <el-form-item label="项目进展" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择项目进展"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="结项状态" prop="closureSituationNew">
         <el-input
          v-model="queryParams.closureSituationNew"
          placeholder="请输入结项状态"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="viewList"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="viewList">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-col :span="1.5">
        <div style="color:red;height: 28px;line-height: 28px;margin-top:-13px;">如需添加项目，请联系该项目的项目负责人添加！</div>
      </el-col>
    <div class="tablebox">
      <el-table
        stripe
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        :default-sort="{prop: 'startDate', order: 'descending'}"
        @sort-change="tableSortChange"
      >
        <el-table-column label="关注" width="50">
          <template slot-scope="scope">
            <el-rate v-model="scope.row.isFocus" :max="1" @change="rateChange(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column
          label="项目名称"
          prop="name"
          align="left"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
          min-width="230"
        />
        <el-table-column
          label="项目代码"
          prop="code"
          align="center"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        />
        <el-table-column
          label="项目进展"
          prop="status"
          align="center"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
          width="140"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.statusType" disable-transitions>{{ scope.row.statusText }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
        label="结项状态"
        align="center"
        prop="closureSituationNew"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
        width="140"
      >
      </el-table-column>
        <el-table-column
          label="开始服务日期"
          prop="startDate"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
          width="140"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startDate,'{y}-{m}-{d} ') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="项目执行部门"
          prop="executeDept"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        />
        <el-table-column
          label="委托单位"
          align="left"
          prop="party"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        />
        <el-table-column
          label="项目负责人"
          prop="projectLeaderName"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
          width="110"
        />
        <el-table-column label="项目人数" width="75" >
          <template slot-scope="scope">
            <a @click="handleProjectMemberView(scope.$index, scope.row)" title="跳转至项目成员">
              {{scope.row.personnelNo}}
            </a>
          </template>
        </el-table-column>
        <el-table-column label="业务数" width="75">
          <template slot-scope="scope">
            <a @click="handleBusinessView(scope.$index, scope.row)" title="跳转至业务清单">
              {{scope.row.businessCount}}
            </a>
          </template>
        </el-table-column>
        <el-table-column width="120" label="操作" align="center">
          <template slot-scope="scope">
            <a @click="handleView(scope.$index, scope.row)">
              <i class="el-icon-office-building aicon" />项目主页
            </a>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="viewList"
      />
    </div>
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>
<script>
import {
  listProject,
  addOrDelfocus
} from '@/api/projectWorkbench/projectGlance';
import aTitle from '@/components/Title';
import { mapGetters } from 'vuex';
export default {
  name: 'ProjectGlance',
  components: { aTitle },
  computed: {
    ...mapGetters(['permission_routes'])
  },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      loading: true,
      total: 0,
      projectRoutes: [],
      statusOptions: [],
      tableData: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        party: undefined,
        name: undefined,
        projectLeader: undefined,
        status: undefined,
        projectLeaderName: undefined,
        closureSituationNew:undefined,
        code:undefined
      }
    }
  },
  created () {
    this.queryParams.status =
      sessionStorage.getItem('projectStatus') || undefined
    if (sessionStorage.getItem('projectStatus') === 'undefined') {
      this.queryParams.status = undefined
    }
    this.viewList()
    this.getDicts('sys_project_status').then(response => {
      this.statusOptions = response.data
    })
  },
  beforeRouteLeave (to, from, next) {
    sessionStorage.removeItem('projectStatus')
    next()
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}项目工作台/业务系统操作说明-项目工作台.xlsx`
      )}&name=${encodeURIComponent("业务系统操作说明-项目工作台.xlsx")}`;
    },
    viewList () {
      this.loading = true
      //查询项目列表
      this.queryParams.userId = JSON.parse(
        sessionStorage.getItem('userinfo') || {}
      ).guid
      listProject(this.addDateRange(this.queryParams, this.dateRange)).then(
        res => {
          if (res.code === 200) {
            this.total = res.total
            this.tableData = res.rows.map(item => {
              if (item.isvalid === '0') {
                item.statusText = '作废';
                item.statusType = 'danger';
              } else {
                if (item.status === '0') {
                  item.statusText = '筹备';
                  item.statusType = '';
                } else if (item.status === '1') {
                  item.statusText = '进行中';
                  item.statusType = 'info';
                } else if (item.status === '2') {
                  item.statusText = '成果交付';
                  item.statusType = 'success';
                } else if (item.status === '3') {
                  item.statusText = '项目完成';
                  item.statusType = 'success';
                } else if (item.status === '4') {
                  item.statusText = '项目暂停';
                  item.statusType = 'danger';
                } else if (item.status === '5') {
                  item.statusText = '项目终止';
                  item.statusType = 'warning';
                } else if (item.status === '6') {
                  item.statusText = '项目作废';
                  item.statusType = 'danger';
                }
              }
              return item
            })
            this.loading = false
          }
        }
      )
    },
    reset () {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 20
      this.queryParams.party = undefined
      this.queryParams.name = undefined
      this.dateRange = []
      this.queryParams.projectLeaderName = undefined
      this.queryParams.status = undefined
      this.queryParams.closureSituationNew = undefined
      this.queryParams.code = undefined
      this.viewList()
    },

    handleView (index, row) {
      this.permission_routes.forEach(item => {
        if (item.name === 'ProjectList') {
          this.projectRoutes = item.children
        }
      })
      this.projectRoutes.map(item => {
        if (item.children) {
          item.children.map(obj => {
            obj.name = obj.name.replace('/:id', '')
            obj.path =
              obj.name.replace(item.name[0], item.name[0].toLowerCase()) +
              '/' +
              row.guid
          })
        } else {
          item.name = item.name.replace('/:id', '')
          item.path =
            item.name.replace(item.name[0], item.name[0].toLowerCase()) +
            '/' +
            row.guid
        }
      })

      console.log(this.projectRoutes)
      sessionStorage.setItem('menuTitle', row.name)
      sessionStorage.setItem('isvalid', row.isvalid)
      sessionStorage.setItem(
        'projectRoutes',
        JSON.stringify(this.projectRoutes)
      )
      let routeData = this.$router.resolve({
        path: `/projectList/projectGeneral/${row.guid}`
      })
      var win = window.open(routeData.href, '_blank');
       win.document.title =  "hello";
    
    },
    rateChange (row) {
      const focus = {}
      focus.guid = row.projectFocusId
      focus.projectId = row.guid
      addOrDelfocus(focus).then(res => {
        if (row.projectFocusId) {
          this.msgSuccess('取消关注项目成功')
        } else {
          this.msgSuccess('关注项目成功')
        }
        this.viewList()
      })
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1
      if (column.order === 'descending') {
        this.queryParams.sortBy = column.prop
        this.queryParams.order = 'desc';
      } else {
        this.queryParams.sortBy = column.prop
        this.queryParams.order = 'asc';
      }
      this.viewList()
    },
    handleProjectMemberView(index, row){
      
      this.permission_routes.forEach(item => {
        if (item.name === 'ProjectList') {
          this.projectRoutes = item.children
        }
      })
      this.projectRoutes.map(item => {
        if (item.children) {
          item.children.map(obj => {
            obj.name = obj.name.replace('/:id', '')
            obj.path =
              obj.name.replace(item.name[0], item.name[0].toLowerCase()) +
              '/' +
              row.guid
          })
        } else {
          item.name = item.name.replace('/:id', '')
          item.path =
            item.name.replace(item.name[0], item.name[0].toLowerCase()) +
            '/' +
            row.guid
        }
      })

      console.log(this.projectRoutes)
      sessionStorage.setItem('menuTitle', row.name)
      sessionStorage.setItem('isvalid', row.isvalid)
      sessionStorage.setItem(
        'projectRoutes',
        JSON.stringify(this.projectRoutes)
      )
      let routeData = this.$router.resolve({
        path: `/projectList/member/${row.guid}`
      })
      var win = window.open(routeData.href, '_blank');
    },
    handleBusinessView(index, row){
      
      this.permission_routes.forEach(item => {
        if (item.name === 'ProjectList') {
          this.projectRoutes = item.children
        }
      })
      this.projectRoutes.map(item => {
        if (item.children) {
          item.children.map(obj => {
            obj.name = obj.name.replace('/:id', '')
            obj.path =
              obj.name.replace(item.name[0], item.name[0].toLowerCase()) +
              '/' +
              row.guid
          })
        } else {
          item.name = item.name.replace('/:id', '')
          item.path =
            item.name.replace(item.name[0], item.name[0].toLowerCase()) +
            '/' +
            row.guid
        }
      })

      console.log(this.projectRoutes)
      sessionStorage.setItem('menuTitle', row.name)
      sessionStorage.setItem('isvalid', row.isvalid)
      sessionStorage.setItem(
        'projectRoutes',
        JSON.stringify(this.projectRoutes)
      )
      let routeData = this.$router.resolve({
        path: `/projectList/businessManageList/businessManagement/${row.guid}`
      })
      var win = window.open(routeData.href, '_blank');
    }
  }
}
</script>
<style lang="scss" scoped>
.tablebox {
  height: 100%;
}
/deep/.el-table .cell {
  font-family: PingFangSC-Regular;
  color: rgba(0, 0, 0, 0.85);
  letter-spacing: 0;
  // text-align: center;
  a {
    font-family: PingFangSC-Regular;
    font-size: 14px;
    color: #1890ff;
    letter-spacing: 0;
    text-align: right;
    line-height: 14px;
    margin: 0 10px;
  }
}
// /deep/.el-table_1_column_4{
//   text-align: center;
// }

.tipTag {
    height: 40px;
    line-height: 40px;
    margin-top: -10px;
}
</style>
